<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class BrandItemsController extends BackendController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('BrandItems');
        $this->_cate_mod = D('BrandCate');
        $this->_brand_mod = D('Brand');
    }
    public function _before_index()
    {
        $zym_53 = $this->_request('sort', 'trim', 'ordid');
        $zym_52 = $this->_request('order', 'trim', 'DESC');
        $zym_55 = $this->_cate_mod->order($zym_53 . ' ' . $zym_52)->field('id,name')->select();
        $zym_56 = array();
        foreach ($zym_55 as $zym_59) {
            $zym_56[$zym_59['id']] = $zym_59['name'];
        }
        $this->assign('cate_list', $zym_56);
        $zym_58 = $this->_brand_mod->order($zym_53 . ' ' . $zym_52)->field('activityId,title')->select();
        $zym_57 = array();
        foreach ($zym_58 as $zym_59) {
            $zym_57[$zym_59['activityId']] = $zym_59['title'];
        }
        $this->assign('bcate_list', $zym_57);
        $zym_51 = $this->_get('p', 'intval', 1);
        $this->assign('p', $zym_51);
    }
    public function pass()
    {
        if (IS_POST) {
            $zym_50['pass'] = $this->_post('pass', 'intval');
            $zym_44 = $this->_post('ids');
            $zym_43 = explode(',', $zym_44);
            $zym_42['pass'] = $zym_50['pass'];
            $this->_mod->where(array('id' => array('in', $zym_44)))->save($zym_42);
            $this->ajaxReturn(1, L('operation_success'), '', 'pass');
        } else {
            $zym_44 = trim($this->_request('id'), ',');
            $this->assign('ids', $zym_44);
            $zym_45 = $this->fetch();
            $this->ajaxReturn(1, '', $zym_45);
        }
    }
    protected function _search()
    {
        $zym_46 = array();
        ($zym_49 = $this->_request('time_start', 'trim')) && ($zym_46['add_time'][] = array('egt', strtotime($zym_49)));
        ($zym_48 = $this->_request('time_end', 'trim')) && ($zym_46['add_time'][] = array('elt', strtotime($zym_48) + (24 * 60 * 60 - 1)));
        ($zym_47 = $this->_request('pass', 'trim')) && ($zym_46['pass'] = $zym_47);
        ($zym_60 = $this->_request('sellerId', 'trim')) && ($zym_46['sellerId'] = array('like', '%' . $zym_60 . '%'));
        $zym_61 = $this->_request('cate_id', 'intval');
        $zym_74 = '';
        if ($zym_61) {
            $zym_73 = $this->_cate_mod->get_child_ids($zym_61, true);
            $zym_46['cate_id'] = array('IN', $zym_73);
            $zym_72 = $this->_cate_mod->where(array('id' => $zym_61))->getField('spid');
            $zym_74 = $zym_72 ? $zym_72 . $zym_61 : $zym_61;
        }
        $this->assign('search', array('time_start' => $zym_49, 'time_end' => $zym_48, 'cate_id' => $zym_61, 'selected_ids' => $zym_74, 'pass' => $zym_47, 'sellerId' => $zym_60));
        return $zym_46;
    }
    public function move()
    {
        if (IS_POST) {
            $zym_50['pid'] = $this->_post('pid', 'intval');
            $zym_44 = $this->_post('ids');
            $zym_43 = explode(',', $zym_44);
            $zym_42['cate_id'] = $zym_50['pid'];
            $this->_mod->where(array('id' => array('in', $zym_44)))->save($zym_42);
            $this->ajaxReturn(1, L('operation_success'), '', 'move');
        } else {
            $zym_44 = trim($this->_request('id'), ',');
            $this->assign('ids', $zym_44);
            $zym_45 = $this->fetch();
            $this->ajaxReturn(1, '', $zym_45);
        }
    }
    public function _before_add()
    {
        $zym_75 = $_SESSION['pp_admin']['username'];
        $this->assign('author', $zym_75);
        $zym_76 = D('setting')->where(array('name' => 'site_name'))->getField('data');
        $this->assign('site_name', $zym_76);
        $zym_80 = $this->_cate_mod->field('id,name')->where(array('pid' => 0))->order('ordid DESC')->select();
        $this->assign('first_cate', $zym_80);
        $zym_79 = $this->_brand_mod->field('id,title,activityId')->order('id asc')->limit('0,100000')->select();
        $zym_77 = array();
        foreach ($zym_79 as $zym_71 => $zym_59) {
            $zym_77['brandlist'][$zym_71] = $zym_59;
        }
        $this->assign('brandlist', $zym_77['brandlist']);
    }
    public function clear()
    {
        if (IS_POST) {
            if ($_REQUEST['isok'] == '1') {
                $zym_70 = $this->_post('action', 'trim');
                $zym_64 = '1=1';
                if ('clear' == $zym_70) {
                    $zym_63 = CONF_PATH . 'db.php';
                    $zym_62 = (require $zym_63);
                    $zym_65 = 'TRUNCATE TABLE ' . $zym_62['DB_PREFIX'] . 'brand_items ';
                    $zym_66 = M()->execute($zym_65);
                    $this->success(L('clear_success'));
                }
                $this->_mod->where($zym_64)->delete();
                $this->success(L('clear_success'));
            } else {
                $this->success('确认是否要删除？', U('brand_items/clear'));
            }
        } else {
            $this->display();
        }
    }
    public function delete_search()
    {
        if (isset($_REQUEST['dosubmit'])) {
            if ($_REQUEST['isok'] == '1') {
                $zym_64 = '1=1';
                $zym_69 = trim($_POST['keyword']);
                $zym_61 = trim($_POST['cate_id']);
                $zym_61 = trim($_POST['cate_id']);
                $zym_49 = trim($_POST['time_start']);
                $zym_48 = trim($_POST['time_end']);
                $zym_47 = trim($_POST['pass']);
                $zym_68 = trim($_POST['min_price']);
                $zym_67 = trim($_POST['max_price']);
                if ($zym_69 != '') {
                    $zym_64 .= ' AND title LIKE \'%' . $zym_69 . '%\'';
                }
                if ($zym_61 != '' && $zym_61 != 0) {
                    $zym_64 .= ' AND cate_id=' . $zym_61;
                }
                if ($zym_49 != '') {
                    $zym_41 = strtotime($zym_49);
                    $zym_64 .= ' AND add_time>=\'' . $zym_41 . '\'';
                }
                if ($zym_48 != '') {
                    $zym_40 = strtotime($zym_48);
                    $zym_64 .= ' AND add_time<=\'' . $zym_40 . '\'';
                }
                if ($zym_47 != '') {
                    $zym_64 .= ' AND pass=' . $zym_47;
                }
                if ($zym_68 != '') {
                    $zym_64 .= ' AND coupon_price>=' . $zym_68;
                }
                if ($zym_67 != '') {
                    $zym_64 .= ' AND coupon_price<=' . $zym_67;
                }
                $zym_14 = $this->_mod->where($zym_64)->select();
                $zym_44 = '';
                foreach ($zym_14 as $zym_59) {
                    $zym_44 .= $zym_59['id'] . ',';
                }
                if ($zym_44 != '') {
                    $zym_44 = substr($zym_44, 0, -1);
                }
                $this->_mod->where($zym_64)->delete();
                $this->success('删除成功', U('brand_items/clear'));
            } else {
                $this->success('确认是否要删除？', U('brand_items/clear'));
            }
        } else {
            $this->display();
        }
    }
    public function _before_edit()
    {
        $zym_13 = $this->_get('id', 'intval');
        $zym_12 = $this->_mod->field('id,cate_id')->where(array('id' => $zym_13))->find();
        $zym_72 = $this->_cate_mod->where(array('id' => $zym_12['cate_id']))->getField('spid');
        if ($zym_72 == 0) {
            $zym_72 = $zym_12['cate_id'];
        } else {
            $zym_72 .= $zym_12['cate_id'];
        }
        $this->assign('selected_ids', $zym_72);
        $zym_79 = $this->_brand_mod->field('id,title,activityId')->order('id asc')->limit('0,100000')->select();
        $zym_77 = array();
        foreach ($zym_79 as $zym_71 => $zym_59) {
            $zym_77['brandlist'][$zym_71] = $zym_59;
        }
        $this->assign('brandlist', $zym_77['brandlist']);
    }
    public function ajax_upload_img()
    {
        $zym_15 = $this->_get('type', 'trim', 'img');
        if (!empty($_FILES[$zym_15]['name'])) {
            $zym_16 = date('ym/d/');
            $zym_19 = $this->_upload($_FILES['img'], 'brand_items/' . $zym_16, array('width' => '280', 'height' => '280'));
            if ($zym_19['error']) {
                $this->ajaxReturn(0, $zym_19['info']);
            } else {
                $zym_18 = $zym_16 . $zym_19['info'][0]['savename'];
                $zym_17 = array_pop(explode('.', $zym_18));
                $zym_50['img'] = str_replace('.' . $zym_17, '_thumb.' . $zym_17, $zym_18);
                $this->ajaxReturn(1, L('operation_success'), $zym_50['img']);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function ajaxGetItem()
    {
        if (!isset($_REQUEST['link'])) {
            $this->ajaxReturn(0, '未传入商品链接');
        }
        $zym_11 = getInfo($_REQUEST['link']);
        $zym_11['coupon_rate'] = round($zym_11['coupon_price'] / $zym_11['price'] * 10, 1);
        $this->ajaxReturn(1, '', $zym_11);
    }
    public function ajax_getid()
    {
        $zym_10 = $this->_get('url', 'trim');
        $zym_4 = parse_url($zym_10);
        $zym_3 = convertUrlQuery($zym_4['query']);
        $zym_2 = $zym_3['id'];
        if ($this->_mod->where(array('num_iid' => $zym_2))->count()) {
            $this->ajaxReturn(0, '该商品已存在');
        } else {
            $this->ajaxReturn(1, l('operation_success'), $zym_2);
        }
    }
    public function ajaxgetid()
    {
        $zym_10 = $this->_get('url', 'trim');
        if ($zym_10 == '') {
            $this->ajaxReturn(0, l('please_input') . l('correct_itemurl'));
        }
        if (!$this->get_id($zym_10)) {
            $this->ajaxReturn(0, l('please_input') . l('correct_itemurl'));
        }
        $zym_1 = $this->get_id($zym_10);
        $zym_5 = new itemcollect();
        $zym_5->url_parse($zym_10);
        if ($zym_6 = $zym_5->fetch_tb()) {
            $this->ajaxReturn(1, '', $zym_6);
        }
        $this->ajaxReturn(0, l('item_not_excute'));
    }
    public function get_id($zym_10)
    {
        $zym_13 = 0;
        $zym_9 = parse_url($zym_10);
        if (isset($zym_9['query'])) {
            parse_str($zym_9['query'], $zym_8);
            if (isset($zym_8['id'])) {
                $zym_13 = $zym_8['id'];
                return $zym_13;
            }
            if (isset($zym_8['item_id'])) {
                $zym_13 = $zym_8['item_id'];
                return $zym_13;
            }
            if (isset($zym_8['default_item_id'])) {
                $zym_13 = $zym_8['default_item_id'];
            }
        }
        return $zym_13;
    }
    public function http($zym_10)
    {
        set_time_limit(0);
        $zym_19 = file_get_contents($zym_10);
        if ($zym_19 === false) {
            $zym_7 = curl_init();
            curl_setopt($zym_7, CURLOPT_URL, $zym_10);
            curl_setopt($zym_7, CURLOPT_HEADER, false);
            curl_setopt($zym_7, CURLOPT_RETURNTRANSFER, 1);
            $zym_19 = curl_exec($zym_7);
            curl_close($zym_7);
        }
        if (empty($zym_19)) {
            $zym_19 = false;
        }
        return $zym_19;
    }
    public function ajax_gettags()
    {
        $zym_20 = $this->_get('title', 'trim');
        $zym_21 = d('items')->get_tags_by_title($zym_20);
        $zym_34 = implode(',', $zym_21);
        $this->ajaxReturn(1, l('operation_success'), $zym_34);
    }
}
function getInfo($zym_10)
{
    $zym_4 = parse_url($zym_10);
    $zym_3 = convertUrlQuery($zym_4['query']);
    $zym_33['param'] = $zym_3;
    $zym_32 = 'http://hws.m.taobao.com/cache/wdetail/5.0/?id=' . $zym_3['id'];
    $zym_35 = curl_init();
    curl_setopt($zym_35, CURLOPT_URL, $zym_32);
    curl_setopt($zym_35, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($zym_35, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($zym_35, CURLOPT_MAXREDIRS, 2);
    $zym_36 = curl_exec($zym_35);
    curl_close($zym_35);
    if (!$zym_36) {
        $zym_36 = file_get_contents($zym_32);
    }
    $zym_50 = getTaobaoShopInfo($zym_36);
    $zym_50['num_iid'] = $zym_3['id'];
    return $zym_50;
}
function getTaobaoShopInfo($zym_39)
{
    $zym_50 = json_decode($zym_39, true);
    $zym_11 = array();
    $zym_38 = json_decode($zym_50['data']['apiStack'][0]['value'], true);
    $zym_11['title'] = $zym_50['data']['itemInfoModel']['title'];
    $zym_11['volume'] = $zym_38['data']['itemInfoModel']['totalSoldQuantity'];
    $zym_11['coupon_price'] = $zym_38['data']['itemInfoModel']['priceUnits'][0]['price'];
    if (substr_count($zym_11['coupon_price'], '-')) {
        $zym_37 = explode('-', $zym_11['coupon_price']);
        $zym_11['coupon_price'] = min($zym_37[0], $zym_37[1]);
    }
    $zym_11['price'] = $zym_38['data']['itemInfoModel']['priceUnits'][1]['price'];
    if (substr_count($zym_11['price'], '-')) {
        $zym_38 = explode('-', $zym_11['price']);
        $zym_11['price'] = min($zym_38[0], $zym_38[1]);
    }
    $zym_11['cu'] = $zym_38['data']['itemInfoModel']['priceUnits'][0]['name'];
    if (!$zym_11['cu']) {
        $zym_11['cu'] = $zym_38['data']['itemInfoModel']['priceUnits'][0]['tips'][0]['txt'];
    }
    $zym_11['pic_url'] = $zym_50['data']['itemInfoModel']['picsPath'][0];
    $zym_11['pic_url'] = str_replace('_320x320.jpg', '', $zym_11['pic_url']);
    $zym_11['sellerId'] = $zym_50['data']['seller']['userNumId'];
    $zym_11['descInfo'] = $zym_50['data']['descInfo']['briefDescUrl'];
    $zym_31 = new itaoke_https();
    $zym_31->fetch($zym_11['descInfo']);
    $zym_30 = $zym_31->results;
    if (!$zym_30) {
        $zym_30 = file_get_contents($zym_11['descInfo']);
    }
    $zym_24 = json_decode($zym_30, true);
    $zym_23 = $zym_24['data']['images'];
    $zym_22 = '<img class="lazy" src=' . $zym_23[0] . '>';
    $zym_25['desc'] = implode('', $zym_23);
    foreach ($zym_23 as $zym_26) {
        if (strpos($zym_25['desc'], $zym_26)) {
            $zym_29 = '<img class="lazy" src=' . $zym_26 . '>';
            $zym_25['desc'] = str_replace($zym_26, $zym_29, $zym_25['desc']);
            $zym_25['desc'] = str_replace($zym_23[0], '', $zym_25['desc']);
        }
    }
    $zym_11['desc'] = $zym_22 . '' . $zym_25['desc'];
    return $zym_11;
}
function convertUrlQuery($zym_28)
{
    $zym_27 = explode('&', $zym_28);
    $zym_8 = array();
    foreach ($zym_27 as $zym_3) {
        $zym_6 = explode('=', $zym_3);
        $zym_8[$zym_6[0]] = $zym_6[1];
    }
    return $zym_8;
}